# 导包
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
from common.assertutil import common_assert


# 创建测试类
class TestBusiness:
    # 初始化
    token = None
    course_id = None
    fileName = None

    # 前置处理
    def setup(self):
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        self.contract_api = ContractAPI()

    # 后置处理
    def teardown(self):
        pass

    # 01 登录成功
    def test01_login_success(self):
        # ① 获取图片验证码成功
        response = self.login_api.get_verify_code()
        # print(response.json())
        # 断言
        # assert 200 == response.status_code
        # assert "成功" in response.text
        # assert 200 == response.json().get("code")
        common_assert(response)
        # 提取uuid值并保存到变量
        uuid = response.json().get("uuid")
        print(uuid)

        # ② 登录成功
        login_data = {
            "username": "admin",
            "password": "admin123",
            "code": "2",
            "uuid": uuid
        }
        response = self.login_api.login(test_data=login_data)
        # 断言
        # assert 200 == response.status_code
        # assert "成功" in response.text
        # assert 200 == response.json().get("code")
        common_assert(response)
        # 提取登录成功token并保存至类的属性
        TestBusiness.token = response.json().get("token")
        print(TestBusiness.token)

    # 02 添加课程成功
    def test02_insert_course_success(self):
        insert_data = {
            "name": "requests自动化测试001",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2",
            "info": "测试开发提升课01"
        }
        response = self.course_api.insert_course(token666=TestBusiness.token, test_data=insert_data)
        # 断言
        # assert 200 == response.status_code
        # assert "成功" in response.text
        # assert 200 == response.json().get("code")
        common_assert(response)

        # 查询课程
        response = self.course_api.select_course(TestBusiness.token)
        # 断言
        # assert 200 == response.status_code
        # assert "成功" in response.text
        # assert 200 == response.json().get("code")
        common_assert(response)
        # 提取课程ID
        TestBusiness.course_id = response.json().get("rows")[0].get("id")
        print("*" * 80)
        print(TestBusiness.course_id)

        # 修改课程
        update_data = {
            "id": TestBusiness.course_id,
            "name": "rose666",
            "subject": "6",
            "price": 998,
            "applicablePerson": "2",
            "info": "rose666"
        }
        response = self.course_api.update_course(token666=TestBusiness.token, test_data=update_data)
        # 断言
        # assert 200 == response.status_code
        # assert "成功" in response.text
        # assert 200 == response.json().get("code")
        common_assert(response)

        # 删除课程
        response = self.course_api.delete_course(course_id=TestBusiness.course_id, token666=TestBusiness.token)
        # 断言
        # assert 200 == response.status_code
        # assert "成功" in response.text
        # assert 200 == response.json().get("code")
        common_assert(response)

    # 03 上传合同成功
    def test03_upload_contract_success(self):
        test_data = open("../data/test.pdf", "rb")
        response = self.contract_api.upload_contract(token666=TestBusiness.token, file_data=test_data)
        print("*" * 80)
        print(response.json())
        # 断言
        common_assert(response, 200, "成功", 200)
        # 提取合同文件保存路径及名称
        TestBusiness.fileName = response.json().get("fileName")
        print(TestBusiness.fileName)

    # 04 新增合同成功
    def test04_insert_contract_success(self):
        # 新增合同
        insert_data = {
            "name": "测试888",
            "phone": "13611223377",
            "contractNo": "HT12345004",     # 合同编号必须唯一(当前处理1时，手动修改；后天回来之后学习怎么通过数据库进行自动化操作)
            "subject": "6",
            "courseId": 99,
            "channel": "0",
            "activityId": 77,
            "fileName": TestBusiness.fileName
        }
        response = self.contract_api.insert_contract(token666=TestBusiness.token, test_data=insert_data)
        common_assert(response)

        # 查看合同列表
        response = self.contract_api.select_contract_list(TestBusiness.token, "13611223377")
        print(response.json())
        # 断言
        common_assert(response)
